﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using OnlineMealOrdering.Areas.WebSiteManage.Models;
using OnlineMealOrdering.Common;
using OnlineMealOrdering.Domain;
using OnlineMealOrdering.Extends;
using OnlineMealOrdering.Persistence;

namespace OnlineMealOrdering.Areas.WebSiteManage.Controllers
{
    public class UserController : Controller
    {
        public ActionResult UserList(int id = 1)
        {
            using (var ctx = new OnlineStoreDomainContext())
            {
                var list = AccountModel.GetAllUser(id, 15);
                return View(list);
            }
        }

        public ActionResult UserEdit(int id)
        {
            using (var ctx = new OnlineStoreDomainContext())
            {
                var model = UserEntity.GetUser(id).ToAccountModel();
                return View(model);
            }
        }

        public ActionResult SettingUser(AccountModel model)
        {
            using (var ctx = new OnlineStoreDomainContext())
            {
                var user = ctx.EntitySet<UserEntity>().Single(a => a.Id == model.Id);

                var status = (UserStatus)Enum.Parse(typeof(UserStatus), model.TempUserStatus);

                user.SettingUser(model.TempStrRoles, status);
                ctx.SubmitChanges();
                return RedirectToAction("UserList");
            }


        }
    }
}
